﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SecretNumber.Models
{
    public class SecretNumber
    {

        // Fält
        private int? _number;
        private List<GuessedNumber> _guessedNumbers;
        public const int MaxNumberOfGuesses = 7;

        // Egenskaper
        public bool CanMakeGuess
        {
            get
            {
                // Lägg till 'om användaren har gissat rätt'
                if (this.Count > MaxNumberOfGuesses || this.Count >= 7) { return false; } else { return true; }
            }
        }

        public int Count
        {
            get { return this.GuessedNumbers.Count(); }
        }

        public int? Guess { get; set; }

        public IList<GuessedNumber> GuessedNumbers
        {
            get { return _guessedNumbers.AsReadOnly(); }
        }

        public GuessedNumber? LastGuessedNumber
        {
            get { return _guessedNumbers.Last(); }
        }

        public int? Number { get { return this.CanMakeGuess ? _number : null; } private set { _number = (int)value; } }

        // Metoder
        public void Initialize()
        {
            this._guessedNumbers.Clear();
            this.Number = new Random().Next(1,101);
            this.Guess = null;
        }

        /// <summary>
        /// Utför gissningen
        /// </summary>
        /// <returns>Outcome</returns>
        public Outcome MakeGuess()
        {


            if (this.Guess > 100 || Guess < 1)
            {
                throw new ArgumentOutOfRangeException();
            }

            if (this.CanMakeGuess)
            {
                foreach (var item in this.GuessedNumbers)
                {
                    if (item.Number == this.Guess) { return Outcome.OldGuess; }
                }

                if (this.Guess > this.Number) { return Outcome.High; }
                else if (this.Guess < this.Number) { return Outcome.Low; }
                else { return Outcome.Right; }
            }
            return Outcome.NoMoreGuesses;
        }

        /// <summary>
        /// Konstruktor
        /// </summary>
        public SecretNumber()
        {
            this._guessedNumbers = new List<GuessedNumber>(MaxNumberOfGuesses);
            this.Initialize();
            
        }

    }
}